#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "mpi.h"

int
main(int argc, char **argv)
{
	int size, rank;
	int i=0;
	int *sendbuf;
	int *recvbuf;
	int *solutions;
	int success = 1;
	printf("before init\n");

	setbuf(stdout,NULL);
	
	MPI_Init(&argc, &argv);
	
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);

	printf("Size %d, Rank %d\n",size,rank);
	
	sendbuf = (int *)malloc(sizeof(int) * size);
	recvbuf = (int *)malloc(sizeof(int) * size); 
	solutions = (int *)malloc(sizeof(int) * size);
	
	for( i = 0; i < size; i++ )
    {
		sendbuf[i] = rank * size + i;
	}

	for( i = 0; i < size; i++ )
	{
		solutions[i] = rank + size * i;
	}

		 printf("ALL_SEND: [%d, %d]\n",sendbuf[0],sendbuf[1]);

		MPI_Alltoall(sendbuf,1,MPI_INT,recvbuf,1, MPI_INT,MPI_COMM_WORLD);
		success = 1;
		for (i = 0; i < size; i++)
		{
			if( recvbuf[i] != solutions[i] )
				success = 0;
		}
		if (success == 1)
		{
			printf("SUCCESS\n");
		}
		else
		{
			printf("FAILURE\n");
		}

	
	
	MPI_Finalize();
	return 0;
}

